package com.jlboot.model.po.sys;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jlboot.core.base.Models;
import com.jfinal.plugin.activerecord.Model;
import com.jlboot.core.consts.Consts;
import com.jlboot.core.util.LoginUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 51594 on 2022/9/10.
 */
public class SysUser  extends Model<SysUser> {
    public static final SysUser dao = new SysUser().dao();
    public static final String table="sys_user";
    public Models mod = Models.n(this);

    public static String getNextId(){
        return SysIdGenerator.dao.getNextId(table);
    }

    public SysUser setCommon(){
        Date now=new Date();
        this.set("id",SysIdGenerator.dao.getNextId(table));
        this.set("del_tag", Consts.COMMON_NONE_DEL);
        this.set("create_time",now);
        this.set("update_time",now);
        this.set("create_by", LoginUtil.getUserId());
        this.set("update_by",LoginUtil.getUserId());
        return this;
    }


    public Page<Record> queryPage(Record search){
        StringBuilder sqlExceptSelect=new StringBuilder("from sys_user a "
                + " "
                + " where a.del_tag=0 "
        );
        List<Object> list=new ArrayList<>();

        if(StrKit.notBlank(search.getStr("search_content"))){
            sqlExceptSelect.append(" and (a.user_name like ?) ");
            list.add("%"+search.getStr("searchContent")+"%");
        }

        if(StrKit.notBlank(search.getStr("user_name"))){
            sqlExceptSelect.append(" and (a.user_name like ?) ");
            list.add("%"+search.getStr("user_name")+"%");
        }

        if(StrKit.notBlank(search.getStr("login_id"))){
            sqlExceptSelect.append(" and (a.login_id like ?) ");
            list.add("%"+search.getStr("login_id")+"%");
        }

        sqlExceptSelect.append(" order by id desc");
        Page<Record> page= Db.paginate(search.getInt("pageNumber"),search.getInt("pageSize")
                ,"select a.* "
                ,sqlExceptSelect.toString(),list.toArray());

        return page;
    }

}

